package com.netsuite.dao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;
import javax.xml.soap.SOAPException;

import com.netsuite.constants.Constants;
import com.netsuite.helper.ErrorHelper;
import com.netsuite.logger.LoggerImp;
import com.netsuite.webservices.platform.common_2014_1.TransactionSearchBasic;
import com.netsuite.webservices.platform.core_2014_1.Record;
import com.netsuite.webservices.platform.core_2014_1.SearchEnumMultiSelectField;
import com.netsuite.webservices.platform.core_2014_1.SearchResult;
import com.netsuite.webservices.platform.core_2014_1.SearchStringField;
import com.netsuite.webservices.platform.core_2014_1.types.SearchEnumMultiSelectFieldOperator;
import com.netsuite.webservices.platform.core_2014_1.types.SearchStringFieldOperator;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRecordCountFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRequestLimitFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededRequestSizeFault;
import com.netsuite.webservices.platform.faults_2014_1.ExceededUsageLimitFault;
import com.netsuite.webservices.platform.faults_2014_1.InvalidCredentialsFault;
import com.netsuite.webservices.platform.faults_2014_1.InvalidSessionFault;
import com.netsuite.webservices.platform_2014_1.NetSuitePortType;
import com.netsuite.webservices.transactions.customers_2014_1.CustomerDeposit;

public class CustomerDepositDAO {

	private static String CLASS_NAME = CustomerDepositDAO.class.getName();

	private NetSuitePortType _service = null;

	public CustomerDepositDAO() throws ErrorHelper {
		try {
			MainDAO mainDAO = new MainDAO();
			_service = mainDAO.get_service();
		} catch (FileNotFoundException e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_FNF + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_FNF
					+ e.getMessage());
		} catch (ServiceException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_SE + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_SE
					+ e.getMessage());
		} catch (IOException e) {
			LoggerImp.errorLog(CLASS_NAME, Constants.ERROR_IO + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_IO
					+ e.getMessage());
		} catch (SOAPException e) {
			LoggerImp.errorLog(CLASS_NAME,
					Constants.ERROR_SOAP + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_SOAP
					+ e.getMessage());
		}

	}

	public String getDepositInternalId(String depositId) throws ErrorHelper {

		LoggerImp.startLog(CLASS_NAME, "getDepositInternalId()");

		String id = "";

		TransactionSearchBasic txnSearch = new TransactionSearchBasic();
		txnSearch.setType(new SearchEnumMultiSelectField());
		txnSearch.getType().setOperator(
				SearchEnumMultiSelectFieldOperator.anyOf);
		txnSearch.getType().setSearchValue(new String[1]);
		txnSearch.getType().setSearchValue(0, "_customerDeposit");
		txnSearch.setTranId(new SearchStringField());
		txnSearch.getTranId().setSearchValue(depositId);
		txnSearch.getTranId().setOperator(SearchStringFieldOperator.contains);

		SearchResult searchResult;
		try {
			searchResult = _service.search(txnSearch);
			if (searchResult.getStatus().isIsSuccess()) {
				for (Record rec : searchResult.getRecordList().getRecord()) {
					CustomerDeposit dep = (CustomerDeposit) rec;
					id = dep.getInternalId();
				}
			}
		} catch (ExceededRequestSizeFault e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_SIZ + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_SIZ
					+ e.getMessage());
		} catch (InvalidCredentialsFault e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_INV + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_INV
					+ e.getMessage());
		} catch (ExceededUsageLimitFault e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_USE + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_USE
					+ e.getMessage());
		} catch (ExceededRequestLimitFault e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_REQ + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_REQ
					+ e.getMessage());
		} catch (ExceededRecordCountFault e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_MAX + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_MAX
					+ e.getMessage());
		} catch (InvalidSessionFault e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_SES + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_SES
					+ e.getMessage());
		} catch (RemoteException e) {
			LoggerImp
					.errorLog(CLASS_NAME, Constants.ERROR_REM + e.getMessage());
			throw new ErrorHelper(CLASS_NAME, Constants.ERROR_SES
					+ e.getMessage());
		}

		LoggerImp.endLog(CLASS_NAME, "getDepositInternalId()");

		return id;
	}

}
